<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.username" clearable class="filter-item" style="width: 200px;" placeholder="请输入用户名" />
      <el-input v-model="listQuery.userId" clearable class="filter-item" style="width: 200px;" placeholder="请输入用户Id" />
      <el-input v-model="listQuery.mobile" clearable class="filter-item" style="width: 200px;" placeholder="请输入手机号" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" width="100px" label="用户ID" prop="id" sortable />

      <el-table-column align="center" label="用户名" prop="userid" />

      <el-table-column align="center" label="手机号码" prop="phone" />

      <el-table-column align="center" label="积分" prop="point">
        <!--<template slot-scope="scope">
          <el-tag >{{ genderDic[scope.row.gender] }}</el-tag>
        </template>-->
      </el-table-column>

      <el-table-column align="center" label="优惠卷" prop="preferentialcard" />

      <el-table-column align="center" label="余额" prop="money">
        <!--<template slot-scope="scope">
          <el-tag >{{ levelDic[scope.row.userLevel] }}</el-tag>
        </template>-->
      </el-table-column>
      <el-table-column align="center" label="等级" prop="level">
        <template slot-scope="scope">
          <el-tag>{{ levelDic[scope.row.level] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="性别" prop="sex">
        <template slot-scope="scope">
          <el-tag>{{ genderDic[scope.row.sex] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" prop="state">
        <template slot-scope="scope">
          <el-tag>{{ statusDic[scope.row.state] }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="生日" prop="birthday" />
      <el-table-column align="center" label="邮箱" prop="email" />
      <el-table-column align="center" label="操作" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleDetail(scope.row)">详情</el-button>
          <el-button type="primary" size="mini" @click="CZhandleDetail(scope.row)">充值</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="listQuery.infoCount>0" :total="listQuery.infoCount" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <!-- 用户编辑对话框 -->
    <el-dialog :visible.sync="userDialogVisible" title="用户编辑">
      <el-form ref="userDetail" :model="userDetail" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="用户名" prop="id">
          <el-input v-model="userDetail.id" :disabled="true" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="userid">
          <el-input v-model="userDetail.userid" />
        </el-form-item>
        <el-form-item label="用户手机" prop="phone">
          <el-input v-model="userDetail.phone" />
        </el-form-item>
        <el-form-item label="用户性别" prop="sex">
          <el-select v-model="userDetail.sex" placeholder="请选择"><el-option v-for="(item, index) in genderDic" :key="index" :label="item" :value="index" /></el-select>
        </el-form-item>
        <el-form-item label="用户等级" prop="level">
          <el-select v-model="userDetail.level" placeholder="请选择"><el-option v-for="(item, index) in levelDic" :key="index" :label="item" :value="index" /></el-select>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="userDetail.state" placeholder="请选择"><el-option v-for="(item, index) in statusDic" :key="index" :label="item" :value="index" /></el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="userDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleUserUpdate">确定</el-button>
      </div>
    </el-dialog>

    <!-- 用户充值对话框 -->
    <el-dialog :visible.sync="CZDialogVisible" title="用户充值(后台测试用)">
      <el-form ref="testDetail" :model="testDetail" status-icon label-position="left" label-width="150px" style="width: 400px; margin-left:50px;">
        <el-form-item label="用户名" prop="id">
          <el-input v-model="testDetail.id" :disabled="true" />
        </el-form-item>
        <h3>注意:余额不能超过9999999</h3>
        <el-form-item label="用户余额" prop="money">
          <el-input v-model="testDetail.money" :disabled="true" />
        </el-form-item>
      <el-form-item label="请输入充值金额:" prop="money">
          <el-input v-model="chargeMoneyObj.money" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="CZDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="CZUserUpdate">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, userDetail, updateUser, chargeMoney } from '@/api/user'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'User',
  components: { Pagination },
  data() {
    return {
      chargeMoneyObj:{
        id:'',
        money:''
      },
      list: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        username: undefined,
        mobile: undefined,
        userId: undefined,
        sort: 'add_time',
        order: 'desc',
        infoCount: undefined
      },
      downloadLoading: false,
      genderDic: ['男', '女', '未知'],
      levelDic: ['普通用户', '高级用户', '特级vip用户', '至尊vip用户', '超级无敌vip用户'],
      statusDic: ['可用', '禁用', '注销'],
      userDialogVisible: false,
      CZDialogVisible:false,
      userDetail: {// eslint-disable-line no-unused-vars

      },
      testDetail:{// eslint-disable-line no-unused-vars

      }
    }
  },
  created() {

    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      /* if (this.listQuery.userId) {
        console.log('8888')
        userDetail(this.listQuery.userId).then(response => {
          this.list = []
          if (response.data.data) {
            this.list.push(response.data.data)
            this.total = 1
            this.listLoading = false
          } else {
            this.list = []
            this.total = 0
            this.listLoading = false
          }
        }).catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
      } else {*/
      fetchList(this.listQuery).then(res => {
        this.list = res.data.data[0]
        this.listQuery.infoCount = res.data.data[1].infoCount


        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['用户名', '手机号码', '性别', '生日', '状态']
        const filterVal = ['username', 'mobile', 'gender', 'birthday', 'status']
        excel.export_json_to_excel2(tHeader, this.list, filterVal, '用户信息')
        this.downloadLoading = false
      })
    },
    handleDetail(row) {
      this.userDetail = row
      this.userDialogVisible = true
    },
    CZhandleDetail(row){
      this.CZDialogVisible = true
      this.testDetail =row
    },
    handleUserUpdate() {
      updateUser(this.userDetail)
        .then((res) => {
         /* this.userDialogVisible = false
          this.$notify.success({
            title: '成功',
            message: '更新用户成功'

          })*/

         if(res.data.msg === '200'){
           this.userDialogVisible = false
            alert("更新成功")
         }else if(res.data.msg === '4001'){
           alert("更新失败")
         }

        })
        .catch(response => {
           alert("更新失败")
        })
    },
    CZUserUpdate(){
      this.chargeMoneyObj.id =this.testDetail.id;
     /* if(this.testDetail.money + this.chargeMoneyObj.money  >9999999){
        alert("充值大于上限")
        this.chargeMoneyObj.money =''
        return
      }*/
     chargeMoney(this.chargeMoneyObj).then(res =>{
      if(res.data.msg == '200'){
         this.userDialogVisible = false
          alert("充值成功")
           this.chargeMoneyObj.money =''
         this.CZDialogVisible=false
          this.getList()
       }else if(res.data.msg == '4001'){
         alert("充值失败")
          this.chargeMoneyObj.money =''
       }

      })
      .catch(response => {
         alert("充值失败")
          this.chargeMoneyObj.money =''
      })

    }
  }
}
</script>
